<template>
  <div >
    <BlogHeader :active-index="'5'"></BlogHeader>
    <el-form ref="form" :model="form" label-width="80px" class="main">
      <el-form-item label="昵称">
        <el-input v-model="form.name" style="max-width: 15rem" ></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.psw" style="max-width: 15rem" show-password="show-password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click.prevent="onsubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>



</template>

<script>
import BlogHeader from "../components/BlogHeader";
import {postLogin} from "../api/api";

export default {
  name: "Login",
  components: {BlogHeader},

  data() {
    return({
      form: {
        name: '',
        psw: '',
      },
      rps: '',
    })
  },

  methods: {
    onsubmit() {
      postLogin(this.form.name, this.form.psw)
        .then(response => {
          console.log(response.data.data.msg)
          if(response.data.data.user)
          {
            this.rps = response
            localStorage.setItem("token", response.headers['x-access-token']);
            localStorage.setItem("username", this.form.name);
            alert('登录成功！')
            this.$router.push({name: "Home"})
          } else {
            alert('用户名或密码错误')
          }



          // 路由跳转
          // 登录成功后回到博客首页

        })
        .catch(error => {
          alert(error.message)
        });
      console.log(this.rps);
    },
  },


}
</script>

<style scoped>

.main {
  margin: 6em 1.5rem 0 1.5rem;
  height: 30em;
  width: 30em;
}
</style>
